﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataModel;
using SchoolBusiness;

namespace SchoolAdministration
{
    public partial class StudentList : System.Web.UI.Page
    {
        private readonly Student blstd = new Student();
        private readonly BLRoutine _blroutine = new BLRoutine();
  
       protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlsection.Enabled = false;
                LoadStudentGrid();
                LoadComboBoxes();
              
            }
        }
        public void LoadComboBoxes()
        {
            List<Grade> grade = _blroutine.GetGrade();
            ddlClass.Items.Clear();
            ddlClass.Items.Insert(0, new ListItem("Class", "0"));
            ddlClass.DataSource = grade;
            ddlClass.DataTextField = "ClassName";
            ddlClass.DataValueField = "ClassId";
            ddlClass.DataBind();
          }
        public void LoadSection(int grade)
        {
            List<MStudent> section = _blroutine.GetSection(grade);
            ddlsection.Items.Clear();
            ddlsection.Items.Insert(0, new ListItem("Section", "0"));
            ddlsection.DataSource = section;
            ddlsection.DataTextField = "Section";
            ddlsection.DataValueField = "Section";
            ddlsection.DataBind();

        }
       
        public void LoadStudentGrid()
        {
           List<MStudent> lstd=new List<MStudent>();
            lstd = blstd.GetAllStudent();
            GridViewStd.DataSource = lstd;
            GridViewStd.DataBind();

        }
        public void LoadStudentGrid(List<MStudent> ListStd)
        {
            GridViewStd.DataSource = ListStd;
            GridViewStd.DataBind();

        }
        protected void GridViewStd_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton l = (LinkButton)e.Row.FindControl("DeleteButton");

                l.Attributes.Add("onclick", "javascript:return " +
                     "confirm('Are you sure you want to delete this record " +
                     DataBinder.Eval(e.Row.DataItem, "Studentid") + "')");

            }
        }
        protected void GridViewStd_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            LoadStudentGrid();
        }
        protected void GridViewStd_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //int index = 1;
            //  int subjectId = (int)GridViewSubject.DataKeys[e.RowIndex].Value;
            //DeleteRecordByID(subjectId);
        }



        protected void GridViewStd_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = GridViewStd.SelectedRow;
        }

        protected void GridViewStd_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int studentId = Convert.ToInt32(e.CommandArgument);
                blstd.DeleteStudentbyId(studentId);
                LoadStudentGrid();
            }
        }

        protected void ddlClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlsection.Enabled = true;
            int gr=int.Parse(ddlClass.SelectedValue.ToString());
            LoadSection(gr);
            List<MStudent> stdList = blstd.GetAllStudent().Where(x => x.Grade == gr).ToList();
            LoadStudentGrid(stdList);

        }

        protected void ddlsection_SelectedIndexChanged(object sender, EventArgs e)
        {
            int gr = int.Parse(ddlClass.SelectedValue.ToString());
            string sec = ddlsection.Text;
            List<MStudent> stdList = blstd.GetAllStudent().Where(x => x.Grade == gr && x.section==sec).ToList();
            LoadStudentGrid(stdList);
        }
        public void Search(string searchString)
        {
            if (!(searchString.Length > 0) )
            {
                List<MStudent> stdList = blstd.GetAllStudent();
            }
            else
            {
                List<MStudent> stdList = blstd.GetAllStudent();
                List<MStudent> lststd = (from student in stdList
                                         where student.FirstName.StartsWith(searchString,
                    StringComparison.OrdinalIgnoreCase)
                || student.LastName.StartsWith(searchString,
                    StringComparison.OrdinalIgnoreCase)
                    || student.MiddleName.StartsWith(searchString, StringComparison.OrdinalIgnoreCase)
                                         orderby student.Roll
                                         select student).ToList();
                foreach (var filteredname in stdList)
                {
                    LoadStudentGrid(lststd);
                }
            }
        }

        protected void txtSearch_TextChanged(object sender, EventArgs e)
        {
            string searchSt=txtSearch.Text;
            Search(searchSt);
        }

             
        }
    }
